## traditional MM Likert outcome (coder race correlated w photos observed)
## only target photos used for analysis
##
## August 9, 2019
##
## Kevin Quinn
## University of Michigan
##

library(MCMCpack)
set.seed(566414)

mydata <- read.csv("../ScaleRaceSpring2017clean.csv")

## subset data
mydata <- mydata[(mydata$condition %in% c("P2B", "P2W") & mydata$R.race == "Black") | (mydata$condition %in% c("P1B", "P1W") & mydata$R.race == "White"),]



## keep target responses and photo IDs but nothing else
target.inds <- c(6, 7, 10, 12, 15, 17, 19, 20, 22, 23)
keep.vars <- c(paste("X", target.inds, "_Q339", sep=""),
               paste("X", target.inds, "a.id", sep=""))

mydata.sub <- mydata[, keep.vars]




## reshape into long format
mydata.sub.long <- reshape(mydata.sub, direction="long",
                           varying=list(c(1:10), c(11:20)),
                           v.names=c("Y", "photoID"),
                           ids=rownames(mydata.sub))

## keep only obs with non-missing values for MM
mydata.sub.long <- na.omit(mydata.sub.long)

mydata.sub.long <- mydata.sub.long[sample(1:nrow(mydata.sub.long),
                                          size=1000, replace=FALSE),]


cat("\n\nN =", nrow(mydata.sub.long), "\n\n") 


b1.M1.out <- MCMCoprobit(Y ~ as.factor(photoID), data=mydata.sub.long,
                         burnin=50000, mcmc=1000000, thin=50, tune=0.05,
                         verbose=10000, seed=102518
                         )


save(b1.M1.out, file="MM3.2.b1.M1.out.Rda")
